<?php
namespace app\index\controller;

class Order extends Base
{
    //获取支付二维码方法地址
    public $url = 'http://www.taoerhuo.top/bishe/vendor/alipay/f2fpay/qrpay.php';

    /*
     * 点击成为好货推荐 生成订单
     */
    public function createOrder()
    {
        $data = input('post.');

        if (!$data['or_pay_uid'] || !$data['or_pay_gid']) {
            $info['code']    = '400';
            $info['message'] = '数据丢失';
            echo displayJson($info);die;
        }

        $data['or_no']         = date('YmdHis').rand(100000000,999999999); //订单号
        $data['or_create_tme'] = time();

        $data['or_amount'] = __GOOD_AMOUNT__;

        $createOrder = db('order',[],false)->insertGetId($data);

        if ($createOrder) {
            //去获取支付二维码
            $param = array(
                'out_trade_no' => $data['or_no'],
                'subject'      => '好货推荐',
                'total_amount' => $data['or_amount']
            );

            //生成待请求数据字符串
            $signStr = createSignStr($param,false,false,false,'&');

            //去curl请求
            $json_result = @sendHttpRequest($signStr,$this->url);

            $result      = json_decode($json_result,true);

            if ($result['code'] == 200) {
                $info['code']     = '200';
                $info['message']  = '订单创建成功';
                $info['url']      = $result['url']; //支付二维码地址
                $info['order_id'] = $createOrder;
                echo displayJson($info);die;
            } else {
                $info['code']    = '400';
                $info['message'] = $result['message'];
                echo displayJson($info);die;
            }

        } else {
            $info['code']    = '400';
            $info['message'] = '订单创建失败';
            echo displayJson($info);die;
        }
    }

    public function test()
    {
        echo   '<img src="http://qr.topscan.com/api.php?logo=http://www.taoerhuo.top/bishe/public/image/tao.png&text=https://qr.alipay.com/bax03392aopmnbxzdnhk60ca"  width="200" height="200" />';
    }

    /*
     * 获取订单支付状态 ajax每隔1秒请求一次
     */
    public function getOrderStatus()
    {
        $or_id = input('order_id');

        if (!$or_id) {
            $info['code']    = '400';
            $info['message'] = '订单id丢失';
            echo displayJson($info);die;
        }

        $order = db('order',[],false)->where('or_id',$or_id)->find();

        if ($order) {
            if ($order['or_pay_status'] == 2) {
                $info['code']    = '200';
                $info['message'] = '支付成功';
                echo displayJson($info);die;
            } else {
                $info['code']    = '400';
                $info['message'] = '未完成支付';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '订单不存在';
            echo displayJson($info);die;
        }
    }

}